package com.example.teesystem.controller;

import cn.hutool.core.lang.Validator;
import com.example.teesystem.common.Permission.PermissionCheck;
import com.example.teesystem.common.utils.PermissionUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.Assistant;
import com.example.teesystem.entity.Permission;
import com.example.teesystem.entity.vo.assistant.AssistantUpdateVo;
import com.example.teesystem.entity.vo.assistant.AssistantUploadVo;
import com.example.teesystem.entityDto.AssistantDto;
import com.example.teesystem.mapper.AssistantMapper;
import com.example.teesystem.service.AssistantService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/tee/assistant",produces = "application/json; charset=utf-8" )
@RequiredArgsConstructor
@Validated//valid无法验证列表中的对象，需要添加@Validated
public class AssistantController {
    private final AssistantService assistantService;
    private final AssistantMapper assistantMapper;

    /**
     * 添加助教
     * @Param AssistantUploadVo 助教信息
     * @return
     */
    @PermissionCheck(PermissionUtil.ASSISTANT_MANAGER)
    @PostMapping("/add")
    public Response add(@Valid @RequestBody(required = false) AssistantUploadVo assistantUploadVo){
        return assistantService.add(assistantUploadVo);
    }

    /**
     * 批量添加助教
     * @param assistantUploadVos 助教列表信息
     * @return
     */
    @PostMapping("/addList")
    public Response add(@Valid @RequestBody(required = false) List<AssistantUploadVo> assistantUploadVos){
        return assistantService.add(assistantUploadVos);
    }

    /**
     * 更新助教
     * @param assistantUpdateVo
     * @return
     */
    @PostMapping("/update")
    @PermissionCheck(PermissionUtil.ASSISTANT_MANAGER)
    public Response update(@Valid @RequestBody(required = false)AssistantUpdateVo assistantUpdateVo){
        return assistantService.update(assistantUpdateVo);
    }

    /**
     * 删除某一助教
     * @param map
     * @return
     */
    @PostMapping("/remove")
    public Response remove(@Valid @RequestBody(required = false) Map<String, Integer> map){
        if(map.get("assistantId")!=null&&map.get("courseId")!=null){
            int assistantId = map.get("assistantId");
            int courseId = map.get("courseId");
            if(assistantMapper.getAssistant(assistantId,courseId) == null) {
                return Response.error(ResultCode.PARAMETER_ERROR.getMsg(), ResultCode.PARAMETER_ERROR.getCode());
            }
            return assistantService.remove(assistantMapper.getAssistant(assistantId,courseId).getId());
        }
        else{
            return Response.error(ResultCode.PARAMETER_MISS.getMsg(),ResultCode.PARAMETER_MISS.getCode());
        }
    }

    /**
     * 展示某课程的所有助教
     * @param map
     * @return
     */
    @ApiOperation("查看课程下所有助教")
    @PostMapping("/getList")
    public Response getList(@RequestBody(required = false) Map<String , Integer> map){
        int CourseId = map.get("courseId");
        if(Validator.isNull(CourseId))//参数验证，判断课程id是否存在
        {
            return Response.error(ResultCode.PARAMETER_MISS.getMsg(),ResultCode.PARAMETER_MISS.getCode());
        }
        List<Assistant> assistants = assistantService.getAssistantByCourse(CourseId);
        List<AssistantDto> assistantDtos = new ArrayList<>();
        for(Assistant assistant : assistants){
            Map<Integer,String> permissionMap = PermissionUtil.getAssistantPermissionMap(assistant.getPermission());
            AssistantDto assistantDto = new AssistantDto(assistant,permissionMap);
            assistantDtos.add(assistantDto);
        }
        return Response.ok(assistantDtos);
    }

    @PostMapping("/getPermissionOfCourse")
    @PermissionCheck(PermissionUtil.ASSISTANT_MANAGER)
    public Response getAssistantPermission(@RequestParam("assistantId") Integer assistantId, @RequestParam("courseId") int courseId){
        List<Permission> permissions = assistantService.getAssistantPermissions(courseId, assistantId);
        return Response.ok(permissions);
    }

}

